package com.geek.novel.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.geek.novel.R;
import com.geek.novel.entity.BookRead;
import com.geek.novel.ui.BookImageView;
import com.geek.novel.utils.LogCatUtil;

import java.util.List;

public class RecommendAdapter extends BaseAdapter {

    private List<BookRead> list;

    public RecommendAdapter(List<BookRead> list){
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(position < 0 || position >= getCount()){
            LogCatUtil.INSTANCE.e("???", "出错：position："+position, null);
            return convertView;
        }
        BookRead book = list.get(position);
        Context context = parent.getContext();
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_gallery_recommend_item, parent, false);
        }
        BookImageView iv = convertView.findViewById(R.id.iv_recommend_gallery_image);
//        iv.setImageResource(R.drawable.image_test);
        String bookName = book.getBookName();

        String imgId = book.getImgId();
        iv.setBookName(bookName);
        iv.loadBookImage(imgId);

        TextView tv = convertView.findViewById(R.id.tv_recommend_gallery_book_name);
        tv.setText(bookName);
        return convertView;
    }

}
